package ui;

import javax.swing.*;
import java.awt.*;
import javax.swing.border.EmptyBorder;

import ui.handlers.CustomerHandler;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.rmi.RemoteException;

public class CreateCustomerScreen extends BaseScreen implements ActionListener {

	private JFrame mainFrame;
	private JTextField username_tf, name_tf, street_tf, city_tf, state_tf, zip_tf;
	private JButton submit_btn, clear_btn;
	
	public JButton getSubmitButton()
	{ return submit_btn; }
	
	public JButton getClearButton()
	{ return clear_btn; }
	
	public CreateCustomerScreen()
	{
		mainFrame = new JFrame();
		mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		mainFrame.setSize(650, 550);
		mainFrame.setLayout(new BorderLayout());
		
		doTitle();
		doBody();
		doButtons();
		
		mainFrame.setVisible(true);
	}
	
	private void doTitle()
	{
		String title = "<html><font size=\"5\">Create Customer</font></html>";
		JLabel title_lbl = new JLabel(title);
		mainFrame.add(new JPanel().add(title_lbl), BorderLayout.NORTH);
	}
	
	private void doBody()
	{
		JPanel mainContent_panel = new JPanel();
		mainContent_panel.setLayout(new GridLayout(8, 1));
		
		username_tf = new JTextField(15);
		name_tf = new JTextField(15);
		street_tf = new JTextField(15);
		city_tf = new JTextField(13);
		state_tf = new JTextField(2);
		zip_tf = new JTextField(10);
		
		JPanel userName_panel = new JPanel();
		userName_panel.setLayout(new FlowLayout());
		userName_panel.add(new JLabel("Username: "));
		userName_panel.add(username_tf);
		
		JPanel name_panel = new JPanel();
		name_panel.setLayout(new FlowLayout());
		name_panel.add(new JLabel("Customer name: "));
		name_panel.add(name_tf);
		
		JPanel street_panel = new JPanel();
		street_panel.setLayout(new FlowLayout());
		street_panel.add(new JLabel("Street: "));
		street_panel.add(street_tf);
		
		JPanel cityState_panel = new JPanel();
		cityState_panel.setLayout(new FlowLayout());
		cityState_panel.add(new JLabel("City: "));
		cityState_panel.add(city_tf);;
		cityState_panel.add(new JLabel("State: "));
		cityState_panel.add(state_tf);
		
		JPanel zip_panel = new JPanel();
		zip_panel.setLayout(new FlowLayout());
		zip_panel.add(new JLabel("Zip: "));
		zip_panel.add(zip_tf);
		
		mainContent_panel.add(userName_panel);
		mainContent_panel.add(name_panel);
		mainContent_panel.add(street_panel);
		mainContent_panel.add(cityState_panel);
		mainContent_panel.add(zip_panel);
		
		mainContent_panel.add(new JPanel());
		mainContent_panel.add(new JPanel());
		mainContent_panel.add(new JPanel());
		
		mainFrame.add(mainContent_panel, BorderLayout.CENTER);
	}
	
	private void doButtons()
	{
		submit_btn = new JButton("Submit");
		clear_btn = new JButton("Clear");
		submit_btn.addActionListener(new CustomerHandler(this));
		clear_btn.addActionListener(new CustomerHandler(this));
		
		JPanel submit_panel = new JPanel();
		submit_panel.add(submit_btn);
		submit_panel.setBorder(new EmptyBorder(0, 0, 0, 20));
		
		JPanel button_panel = new JPanel();
		button_panel.setLayout(new FlowLayout());
		button_panel.add(submit_panel);
		button_panel.add(clear_btn);
		
		button_panel.setBorder(new EmptyBorder(0, 0, 100, 0));
		
		mainFrame.add(button_panel, BorderLayout.SOUTH);
	}
	
	public void actionPerformed(ActionEvent e)
	{

	}
	
	public void clear()
	{
		username_tf.setText("");
		name_tf.setText("");
		street_tf.setText("");
		city_tf.setText("");
		state_tf.setText("");
		zip_tf.setText("");
	}
	
	public String createCustomer()
	{
		String cityState = city_tf.getText() +", "+ state_tf.getText();
		try{
			store.createCustomer(name_tf.getText(), street_tf.getText(),
					cityState, zip_tf.getText(), username_tf.getText());
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return username_tf.getText();
		
	}

	public JFrame getMainFrame() {
		return mainFrame;
	}
	
}